﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web;

namespace NRemedy.MVC.UI
{
    public class NRemedyGridColumn<TModel>
    {
        public string propertyId { get; set; }
        public object htmlAttribute { get; set; }
        public object optionAttribute { get; set; }
        public string title { get; set; }

        public NRemedyGridColumn(string propertyId, string title, object htmlAttribute, object optionAttribute)
        {
            this.propertyId = propertyId;
            this.htmlAttribute = htmlAttribute;
            this.optionAttribute = optionAttribute;
            this.title = title;
        }

        public virtual string GetHtml()
        {
            RawTagBuilder Th = new RawTagBuilder("th");

            DataOptionBuilder thopt = new DataOptionBuilder();
            thopt.MergeAttribute("field", "f" + propertyId);

            if (optionAttribute != null)
            {
                var option_dictionary = HtmlHelper.AnonymousObjectToHtmlAttributes(optionAttribute);
                foreach (var option in option_dictionary)
                {
                    thopt.MergeAttribute(option.Key, option.Value);
                }
            }

            Th.MergeAttribute("data-options", thopt.ToString());

            if (htmlAttribute != null)
            {
                var html_dictionary = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttribute);
                foreach (var h in html_dictionary)
                {
                    string value = HttpUtility.HtmlAttributeEncode(h.Value.ToString());
                    Th.MergeAttribute(h.Key, value);
                }
            }

            Th.InnerHtml = HttpUtility.HtmlEncode(title);
            return Th.ToString(TagRenderMode.Normal);
 
        }
    }
}
